package com.my.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.my.cloud.entities.Pay;
import com.my.cloud.entities.PayDTO;
import com.my.cloud.resp.ResultData;
import com.my.cloud.resp.ReturnCodeEnum;
import com.my.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/7 13:03
 */
@RestController
@Slf4j
@RequestMapping("/pay")
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {
    
    @Resource
    private PayService payService;
    
    @Value("${server.port}")
    private String port;
    
    @GetMapping("/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String info){
        return "atguiguInfo:" + info + "\tport: " + port;
    }
    
    @PostMapping("/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        System.out.println("pay.toString() = " + pay.toString());
        int add = payService.add(pay);
        ResultData<String> res = ResultData.success("成功插入记录，返回值：" + add);
        return res;
    }
    
    @DeleteMapping("/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<String> deletePay(@PathVariable("id") Integer id){
        int delete = payService.delete(id);
        ResultData<String> res = ResultData.success("成功删除记录，返回值：" + delete);
        return res;
    }
    
    @PutMapping("/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO, pay);

        int update = payService.update(pay);

        ResultData<String> res = ResultData.success("成功修改记录，返回值：" + update);
        return res;
    }
    
    @GetMapping("/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){

        if (id == -4){
            throw new RuntimeException("id不能为-4");
        }
        
        // 暂停62秒，故意写bug，测试出feign的调用超时时间
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ResultData<Pay> res = ResultData.success(payService.getById(id));

        return res;

    }
    
    @GetMapping("/get")
    public ResultData<List<Pay>> getAll(){
        ResultData<List<Pay>> res = ResultData.success(payService.getAll());
        return res;
    }
    
    @GetMapping("/error")
    public ResultData<Integer> getPayError(){
        
        Integer integer = 200;
        try {
            System.out.println("come in payerror test");
            int age = 10 / 0;
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.failure(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return ResultData.success(integer);
        
    }
    
}
